package com.atguigu.eduservice.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.service.SubjectService;
import com.atguigu.servicebase.exceptionhandler.ELeanExeception;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.Map;

/**
 * @Author panghl
 * @Date 2021/1/4 21:28
 * @Description 读取课程的excel监听器（因为SubjectExcelListener不能交给spring进行管理，需要自己new，不能注入其他对象，不能实现数据库操作）
 **/
public class SubjectExcelListener extends AnalysisEventListener<SubjectData> {

    public SubjectService subjectService;

    public SubjectExcelListener() {
    }

    public SubjectExcelListener(SubjectService subjectService) {
        this.subjectService = subjectService;
    }

    /**
     * 一行一行读取excel
     * @param subjectData
     * @param analysisContext
     */
    @Override
    public void invoke(SubjectData subjectData, AnalysisContext analysisContext) {
        if (subjectData==null){
            throw new ELeanExeception(20001,"文件数据为空");
        }
        EduSubject existOneEduSubject = existOneSubject(this.subjectService, subjectData.getOneSubjectName());
        if (existOneEduSubject == null){
            existOneEduSubject = new EduSubject();
            existOneEduSubject.setParentId("0");
            existOneEduSubject.setTitle(subjectData.getOneSubjectName());
            subjectService.save(existOneEduSubject);
        }
        //获取到一级分类的id值
        String pid = existOneEduSubject.getId();
        EduSubject existTwoEduSubject = existTwoSubject(this.subjectService, subjectData.getTwoSubjectName(), pid);
        if (existTwoEduSubject == null){
            existTwoEduSubject = new EduSubject();
            existTwoEduSubject.setParentId(pid);
            existTwoEduSubject.setTitle(subjectData.getTwoSubjectName());
            subjectService.save(existTwoEduSubject);
        }
    }

    //判断一级分类不能重复添加
    private EduSubject existOneSubject(SubjectService subjectService, String name){
        QueryWrapper<EduSubject> qw = new QueryWrapper<>();
        qw.eq("title",name)
          .eq("parent_id","0")
          .last("limit 1");
        return subjectService.getOne(qw);
    }

    //判断二级分类不能重复添加
    private EduSubject existTwoSubject(SubjectService subjectService, String name, String pid){
        QueryWrapper<EduSubject> qw = new QueryWrapper<>();
        qw.eq("title",name)
                .eq("parent_id",pid)
                .last("limit 1");
        return subjectService.getOne(qw);
    }

    /**
     * 读取表头
     * @param headMap
     * @param context
     */
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        super.invokeHeadMap(headMap, context);
    }

    /**
     * 读取之后
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
